package com.yanqu.road.server.cmd.activity.shuihu;

import com.yanqu.road.entity.activity.shuihu.ShuiHuUserData;
import com.yanqu.road.pb.activity.ShuiHuActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihu.ShuiHuModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.shuihu.ShuiHuMgr;
import com.yanqu.road.server.pb.ShuiHuPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SHUIHU_ACTIVITY_GET_USER_DATA, desc = "获取用户数据")
public class GetShuiHuUserDataCmd extends AbstractCommand {

    private ShuiHuActivityProto.ShuiHuUserDataMsg.Builder userDataBuilder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!ActivityMgr.activityInShowTime(ShuiHuMgr.getActivityInfo())) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_OPEN;
            return;
        }

        ShuiHuModule shuiHuModule = player.getModule(ShuiHuModule.class);
        ShuiHuUserData userData = shuiHuModule.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_UNLOCK;
            return;
        }

        userDataBuilder = ShuiHuPb.parseShuiHuUserDataMsg(userData);
    }

    @Override
    public void sendData(GamePlayer player) {
        ShuiHuActivityProto.GetShuiHuUserDataRespMsg.Builder builder = ShuiHuActivityProto.GetShuiHuUserDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setUserData(userDataBuilder.build());
        }
        player.sendPacket(ClientProtocol.U_SHUIHU_ACTIVITY_GET_USER_DATA, builder);
    }
}
